package com.tanjie.blackbean.iot.simulator.controller;

import cn.hutool.core.util.StrUtil;
import com.tanjie.blackbean.iot.simulator.model.constant.MqttTopicMeta;
import com.tanjie.blackbean.mqtt.support.BlackbeanMqttUtil;
import com.tanjie.blackbean.pojo.exception.BizException;
import com.tanjie.blackbean.schedule.properties.ScheduleConfigProperties;
import com.tanjie.blackbean.schedule.support.BlackbeanSchedule;
import com.tanjie.blackbean.web.annotation.BlackBeanController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledFuture;

/**
 * @Author tanjie
 * @Date 2022/6/21 下午3:18 周二
 **/
@Slf4j
@Tag(name = "传感器的定时任务管理API")
@BlackBeanController(skipAuth = true)
@RequestMapping("sensor")
public class SensorScheduleController {

    @Resource
    BlackbeanSchedule blackbeanSchedule;

    @Operation(summary = "发送消息")
    @GetMapping("send")
    public void sendMqttMessageHandler(String msg, String handler) {
        if (StrUtil.isBlank(handler)) {
            throw new BizException("handler require not blank");
        }
        BlackbeanMqttUtil.send(handler, MqttTopicMeta.SENSOR_UPLOAD_TEMPERATURE + "110", msg);
    }

    @Operation(summary = "查看所有运行中的定时任务")
    @GetMapping("schedule/running")
    public List<String> getRunningScheduleHandler() {
        Map<String, ScheduledFuture<?>> futureMap = blackbeanSchedule.getRunningSchedule();
        return new ArrayList<>(futureMap.keySet());
    }

    @Operation(summary = "查看所有的定时任务配置")
    @GetMapping("schedule/all-config")
    public List<ScheduleConfigProperties.ScheduleConfig> getAllScheduleConfigHandler() {
        return blackbeanSchedule.getScheduleConfigList();
    }

}
